package com.ai.service.made.impl;


import com.ai.networking.netty.handler.ServerHandler;
import com.ai.service.made.AlgorithmCameraService;
import com.ai.service.made.L2SignalService;
import com.ai.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.ai.common.constant.MadeCommonArgs.*;


@Slf4j
@Service
public class L2SignalServiceImpl implements L2SignalService {

    @Resource
    AlgorithmCameraService algorithmCameraService;

    private static String CODE = "l2";

    @Override
    public void analysis(byte[] msg) {
        log.info(StringUtil.bytesToHexString(msg));

        if (msg.length < 15) {
            log.info("消息长度小于15，此信号不做解析");
            return;
        }
        log.info("接收消息解析后内容：" + new String(msg));
        byte[] length = Arrays.copyOfRange(msg, 0, 7);
        String length_s = new String(length);
        byte[] number = Arrays.copyOfRange(msg, 7, 13);
        String number_s = new String(number);
        log.info(length_s + "   " + number_s);

        if (L2_HEARTBEAT_CODE.equals(number_s)) {
            receiveHeartbeat();
            return;
        }
        if (L2_INSTRUCT.equals(number_s)) {
            instruct(msg);
            return;
        }
    }

    @Override
    public void instruct(byte[] msg) {
        byte[] planNo = Arrays.copyOfRange(msg, 27, 47);
        String planNo_s = new String(planNo).trim();

        Map<String, Object> map = new HashMap<>();
        map.put("stripNo", planNo_s);
        algorithmCameraService.bulkSwitchForTelegram(2301, 4, 0, map);
    }

    @Override
    public void returnResults(String planNo, String discernNo, int discernResult) {
        int length = 69;

        byte[] msg = new byte[length];

        // 电文长度 = 69  char 7
        StringUtil.byteInsert(msg, StringUtil.convertStringToHex("00000" + length), 0);

        // 电文号 = L2AI01   char 6
        StringUtil.byteInsert(msg, StringUtil.convertStringToHex(AI_RESULTS), 7);

        // 日期时间    char 14
        String time = SFORMAT.format(new Date());
        StringUtil.byteInsert(msg, StringUtil.convertStringToHex(time), 13);

        // 计划钢卷号    char 14
        StringUtil.byteInsert(msg, StringUtil.convertStringToHex(planNo), 27);

        // 识别钢卷号    char 14
        StringUtil.byteInsert(msg, StringUtil.convertStringToHex(discernNo), 47);

        // 识别结果    char 14
        StringUtil.byteInsert(msg, StringUtil.convertStringToHex(discernResult + ""), 67);

        // 结束符
        msg[length - 1] = END;

        log.info(Arrays.toString(msg));
        log.info("发送识别消息解析后内容：" + new String(msg));

        ServerHandler.sendMessage(CODE, msg);
    }

    @Override
    public void receiveHeartbeat() {
        sendHeartbeat();
    }


    @Override
    public void sendHeartbeat() {

        int length = 34;

        byte[] msg = new byte[length];

        // 电文长度 = 34  char 7
        StringUtil.byteInsert(msg, StringUtil.convertStringToHex("00000" + length), 0);

        // 电文号 = L2AI01   char 6
        StringUtil.byteInsert(msg, StringUtil.convertStringToHex(AI_HEARTBEAT_CODE), 7);

        // 日期时间    char 14
        String time = SFORMAT.format(new Date());
        StringUtil.byteInsert(msg, StringUtil.convertStringToHex(time), 13);

        // 心跳报文 char 6
        StringUtil.byteInsert(msg, StringUtil.convertStringToHex(HEARTBEAT_MESSAGE), 27);

        // 结束符
        msg[length - 1] = END;

        log.info(Arrays.toString(msg));

        ServerHandler.sendMessage(CODE, msg);
    }

    public static void main(String[] args) {

        int length = 34;

        byte[] msg = new byte[length];

        // 电文长度 = 34  char 7
        StringUtil.byteInsert(msg, StringUtil.convertStringToHex("00000" + length), 0);

        // 电文号 = L2AI01   char 6
        StringUtil.byteInsert(msg, StringUtil.convertStringToHex(L2_HEARTBEAT_CODE), 7);

        // 日期时间    char 14
        String time = SFORMAT.format(new Date());
        StringUtil.byteInsert(msg, StringUtil.convertStringToHex(time), 13);

        // 心跳报文 char 6
        StringUtil.byteInsert(msg, StringUtil.convertStringToHex(HEARTBEAT_MESSAGE), 27);

        // 结束符
        msg[length - 1] = END;

        System.out.println(Arrays.toString(msg));
        L2SignalServiceImpl service = new L2SignalServiceImpl();
        service.analysis(msg);
    }
}
